#include "FbxAnimationNode.h"

namespace GC
{
	GC::FbxAnimationNode::FbxAnimationNode( FbxAnimLayer* a_layer, FbxNode* a_node, const double a_deltaFrameTime )
		: m_id(FbxHelper::GetNodeID(a_node)),
		m_name(a_node->GetName())
	{
		const FbxNode* f_parentNode = a_node->GetParent();

		if (f_parentNode != nullptr && !FbxHelper::IsRootNode(f_parentNode))
		{
			m_parentID = FbxHelper::GetNodeID(f_parentNode);
			m_parentName = FbxString( f_parentNode->GetName() );
		}
		else
		{
			m_parentID = -1;
		}		

		m_validNode = ValidateNode( a_layer, a_node, a_deltaFrameTime );
	}	

	/// <summary>
	/// Is valid if m_transform is set
	/// </summary>	
	const bool GC::FbxAnimationNode::IsValid()
	{
		return m_validNode;
	}
	/* Public Get functions for exporter mainly */

	const int GC::FbxAnimationNode::GetID() const
	{
		return m_id;
	}
	const FbxString GC::FbxAnimationNode::GetName() const
	{
		return m_name;
	}

	/// <summary>
	/// Determines whether a parent exists or not for the node
	/// </summary>
	/// <returns></returns>
	const bool GC::FbxAnimationNode::HasParent() const
	{
		return m_parentID != -1;
	}

	/// <summary>
	/// Returns -1 if there is no parent
	/// </summary>
	/// <returns></returns>
	const int GC::FbxAnimationNode::GetParentID() const
	{
		return m_parentID;
	}
	const FbxString GC::FbxAnimationNode::GetParentName() const
	{
		return m_parentName;
	}

	const GC::FbxAnimationTransformPtr GC::FbxAnimationNode::GetTransform() const
	{
		return m_transform;
	}


	/* Private functions */

	/// <summary>
	/// Validates the node by checking if it has any animation data for either of T R S, 
	/// If it does it creates the m_transform object and thus is valid! 
	/// </summary>
	/// <param name="a_layer">The a_layer.</param>
	/// <param name="a_node">The a_node.</param>
	/// <param name="a_deltaFrameTime">The a_delta frame time.</param>
	/// <returns></returns>
	const bool GC::FbxAnimationNode::ValidateNode(FbxAnimLayer* a_layer, FbxNode* a_node, const double a_deltaFrameTime)
	{
		bool f_valid = false;
		
		const GC::FbxAnimationChannelPtr f_translation = std::make_shared<GC::FbxAnimationChannel>(a_layer, a_node, GC::FbxAnimationChannel::ChannelType::Translation, a_deltaFrameTime);
		const GC::FbxAnimationChannelPtr f_rotation = std::make_shared<GC::FbxAnimationChannel>(a_layer, a_node, GC::FbxAnimationChannel::ChannelType::Rotation, a_deltaFrameTime);
		const GC::FbxAnimationChannelPtr f_scale = std::make_shared<GC::FbxAnimationChannel>(a_layer, a_node, GC::FbxAnimationChannel::ChannelType::Scale, a_deltaFrameTime);
		
		if ( f_translation->IsValid() || f_rotation->IsValid() || f_scale->IsValid() )
		{		
			m_transform = std::make_shared<GC::FbxAnimationTransform>( a_node, f_translation.get(), f_rotation.get(), f_scale.get() );

			f_valid = true;
		}	
		
		return f_valid;
	}
}